﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Catering.View_Operaction;
using System.Data;
using System.Web.Services;
using Microsoft.Reporting.WebForms;

namespace Catering.Catering_Page.Kufang
{
    public partial class rengongchengben : System.Web.UI.Page
    {
        public string m_GropuID = "-10";
        public string m_ClassID = "-10";
        public string m_Year = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(rengongchengben));
            if (!IsPostBack)
            {
                if (Session["PersonnelID"] != null)
                {
                    string userid = Session["PersonnelID"].ToString();
                    int companyid = 0;
                    int groupid = 0;
                    int classid = 0;
                    bool isAdmin = false;
                    Bind_Data_to_Controls.GetCompanyByCurrentUser(userid, out companyid, out groupid, out classid, out isAdmin);
                    if (isAdmin)
                    {
                        DataTable dt_zyq = new Catering_Rules.OperationArea().GetAllListByCompanyID(companyid).Tables[0];
                        this.dl_GroupList.LoadDrop(dt_zyq, "id", "AreaName");

                        DataTable dt_bz = new Catering_Rules.ClassInfo().GetList("AreaID=" + this.dl_GroupList.SelectedValue).Tables[0];
                        this.dl_ClassList.LoadDrop(dt_bz, "id", "CName");
                        if (Request.QueryString["AreaID"] != null) m_GropuID = Request.QueryString["AreaID"].ToString();
                        if (Request.QueryString["ClassID"] != null) m_ClassID = Request.QueryString["ClassID"].ToString();
                    }
                    else
                    {
                        Bind_Data_to_Controls.Bind_Area_ddl(dl_GroupList, isAdmin, groupid, companyid);
                        Bind_Data_to_Controls.Bind_Class_ddl(dl_GroupList.SelectedItem, dl_ClassList, isAdmin, classid);
                        m_GropuID = groupid.ToString();
                        m_ClassID = classid.ToString();
                    }
                    m_Year = DateTime.Now.Year.ToString();
                    if (Request.QueryString["Year"] != null) m_Year = Request.QueryString["Year"].ToString();
                    this.GetSearch(m_GropuID, m_ClassID, m_Year);
                    this.DDLTool1.SetAreaID(m_GropuID);
                    this.DDLTool1.SetClassID(m_ClassID);
                    this.DDLTool1.SetYear(m_Year);
                }
            }
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            string OAREAID = DDLTool1.GetDDL_Area();
            string CLASSID = DDLTool1.GetDDL_Class();
            string year = DDLTool1.GetYear();
            GetSearch(OAREAID, CLASSID, year);
        }

        private void GetSearch(string OAREAID, string CLASSID, string year)
        {
            string companyID = "0";
            if (string.IsNullOrEmpty(year))
            {
                return;
            }
            if (Session["UserID"] != null)
            {
                int userid = Convert.ToInt32(Session["UserID"]);
                companyID = new Catering_Rules.T_Personnel().GetCompanyIDByPID(userid).ToString();
            }
            DateTime date = DateTime.MinValue;
            DateTime.TryParse(year + "-1-1", out date);
            string title = Bind_Data_to_Controls.Bind_ReportTitle(OAREAID, CLASSID, Convert.ToDateTime(year + "-1-1 2:11"), "人工成本统计");
            DataTable dt = new Catering.View_Operaction.View_Monitor().GetLaborCostsList(OAREAID, CLASSID, date, companyID, title);
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            ReportViewer1.LocalReport.Refresh();
            ReportViewer1.Visible = true;
        }

        protected void btn_tijiao_Click(object sender, EventArgs e)
        {

        }

        //获取班组Option
        [WebMethod]
        public static string GetBZOption(string strid)
        {
            DataTable dt_bz = new Catering_Rules.ClassInfo().GetList("AreaID=" + strid).Tables[0];
            return dt_bz.GetOption("id", "CName");
        }

        [WebMethod]
        public static string GetAjaxLadorCosts(string StrGroupID, string StrClassID, string StrDate)
        {
            int groupid = 0, classid = 0;
            DateTime date = DateTime.MinValue;
            Int32.TryParse(StrGroupID, out groupid);
            Int32.TryParse(StrClassID, out classid);
            DateTime.TryParse(StrDate, out date);
            if (groupid <= 0) return "1";
            if (classid <= 0) return "2";
            if (date <= DateTime.MinValue) return "3";
            Catering_Model.LaborCostsInfo model = new Catering_Rules.LaborCosts().GetModel(groupid, classid, date);
            if (model != null)
            {
                return model.Number.ToString();
            }
            return "";
        }


        [WebMethod]
        public static string SetAjaxLadorCosts(string StrGroupID, string StrClassID, string StrDate, decimal Number)
        {
            int groupid = 0, classid = 0;
            DateTime date = DateTime.MinValue;
            Int32.TryParse(StrGroupID, out groupid);
            Int32.TryParse(StrClassID, out classid);
            DateTime.TryParse(StrDate, out date);
            if (groupid <= 0) return "1";
            if (classid <= 0) return "2";
            if (date <= DateTime.MinValue) return "3";
            Catering_Model.LaborCostsInfo model = new Catering_Rules.LaborCosts().GetModel(groupid, classid, date);
            if (model != null)
            {
                model = new Catering_Model.LaborCostsInfo();
                model.GroupID = groupid;
                model.ClassID = classid;
                model.Date = date;
                model.Number = Number;
                bool result = new Catering_Rules.LaborCosts().UpdateByWhere(model);
                if (result) return "6"; else return "7";
            }
            else
            {
                model = new Catering_Model.LaborCostsInfo();
                model.GroupID = groupid;
                model.ClassID = classid;
                model.Date = date;
                model.CompanyID = new Catering_Rules.Company().GetModelByID(groupid, false).id;
                model.Number = Number;
                bool result = new Catering_Rules.LaborCosts().Add(model);
                if (result) return "4"; else return "5";
            }

        }

        [WebMethod]
        public static string ExistLadorCosts(string StrGroupID, string StrClassID, string StrDate)
        {
            int groupid = 0, classid = 0;
            DateTime date = DateTime.MinValue;
            Int32.TryParse(StrGroupID, out groupid);
            Int32.TryParse(StrClassID, out classid);
            DateTime.TryParse(StrDate, out date);
            if (groupid <= 0) return "1";
            if (classid <= 0) return "2";
            if (date <= DateTime.MinValue) return "3";
            bool result = new Catering_Rules.LaborCosts().Exists(groupid, classid, date);
            if (result) return "OK"; else return "NO";
        }
    }
}